package com.yunxi.estate.config.core.service.impl;

import com.yunxi.estate.config.core.domain.AddBody;
import com.yunxi.estate.config.core.domain.AjaxResult;
import com.yunxi.estate.config.core.domain.LoginBody;
import com.yunxi.estate.config.core.domain.RegisterBody;
import com.yunxi.estate.config.core.mapper.LoginMapper;
import com.yunxi.estate.config.core.service.ILoginService;
import com.yunxi.estate.config.utils.StringUtils;
import com.yunxi.estate.domain.AddUser;
import com.yunxi.estate.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 登录注册实现
 *
 * @author 松子鱼
 */
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private LoginMapper loginMapper;

    /**
     * 检查Cookies值
     *
     * @param req 请求值
     * @param user 用户对象
     */
    public SysUser CookiesCheck(HttpServletRequest req,SysUser user) {
        for (Cookie cookie : req.getCookies()) {
            if ("phone".equals(cookie.getName())) {
                user.setPhone(cookie.getValue());
            }
            if ("password".equals(cookie.getName())) {
                user.setPassword(cookie.getValue());
            }
        }
        return user;
    }

    /**
     * 添加Cookies值
     *
     * @param session 会话
     * @param user 用户对象
     * @param res 响应值
     */
    public void insertCookies(HttpSession session, SysUser user, HttpServletResponse res) {
        if(user != null) {
            if(Objects.equals(user.getDelFlag(), "0")) {
                session.setAttribute("user", user);
                Cookie phone = new Cookie("phone", user.getPhone());
                phone.setMaxAge(7 * 24 * 60 * 60);
                phone.setHttpOnly(true);
                Cookie password = new Cookie("password", user.getPassword());
                password.setMaxAge(7 * 24 * 60 * 60);
                password.setHttpOnly(true);
                res.addCookie(phone);
                res.addCookie(password);
                System.out.println(AjaxResult.success());
            } else {
                System.out.println(AjaxResult.error(400,"当前账号已冻结"));
            }
        } else {
            System.out.println(AjaxResult.error(400,"用户名或密码不正确"));
        }
    }

    /**
     * 查找登录对象
     *
     * @param loginInfo 登录信息
     * @return 用户对象
     */
    public SysUser selectLoginUser(LoginBody loginInfo) {
        return loginMapper.selectLoginUser(loginInfo);
    }

    /**
     * 注册方法
     *
     * @return 注册状态信息
     */
    public AjaxResult register(RegisterBody register, HttpSession session) {
        SysUser user = new SysUser();
        AddUser addInfo = new AddUser();
        AddBody addVerify = new AddBody();
        String address;
        if (StringUtils.isPhone(register.getPhone())) {
            return AjaxResult.error(400,"手机号格式不正确");
        } else {
            if(StringUtils.isPassword(register.getPassword())) {
                return AjaxResult.error(400,"密码格式不正确");
            } else {
                if (loginMapper.selectUserPhone(register.getPhone()) == null && loginMapper.selectUserName(register.getUserName()) == null) {
                    user.setUserName(register.getUserName());
                    user.setSex(register.getSex());
                    user.setPhone(register.getPhone());
                    user.setPassword(register.getPassword());
                    user.setRoleId(register.getRoleId());
                    addVerify.setEstateName(register.getEstateName());
                    addVerify.setBuildNum(register.getBuildNum());
                    addVerify.setUnitNum(register.getUnitNum());
                    addVerify.setRoomNum(register.getRoomNum());
                    if (loginMapper.selectAddId(addVerify) != null) {
                        address = register.getEstateName() + register.getBuildNum().toString() + "栋" + register.getUnitNum().toString() + "单元" + register.getRoomNum().toString() + "室";
                        loginMapper.insertUser(user);
                        addInfo.setResidentId(loginMapper.selectUserId(user));
                        addInfo.setAddName(address);
                        addInfo.setAddId(loginMapper.selectAddId(addVerify));
                        loginMapper.insertAddress(addInfo);
                        session.setAttribute("user", user);
                        return AjaxResult.success();
                    } else {
                        return AjaxResult.error("该地址不存在,请确认后重新填写");
                    }

                } else if(loginMapper.selectUserPhone(register.getPhone()) != null) {
                    return AjaxResult.error(400,"该手机号已经注册过了,请直接登录吧");
                } else if(loginMapper.selectUserName(register.getUserName()) != null) {
                    return AjaxResult.error(400,"该用户名已经注册过了,请直接登录吧");
                } else {
                    return AjaxResult.error(400,"当前注册用户有未知问题,请联系管理员");
                }
            }
        }
    }

    /**
     * 登录方法
     *
     * @return 登录状态信息
     */
    public AjaxResult login(SysUser loginUser, HttpSession session, HttpServletResponse res) {

        if(loginUser != null) {
            if(Objects.equals(loginUser.getDelFlag(), "0")) {
                session.setAttribute("user", loginUser);
                Cookie phone = new Cookie("phone", loginUser.getPhone());
                phone.setMaxAge(7 * 24 * 60 * 60);
                phone.setHttpOnly(true);
                Cookie password = new Cookie("password", loginUser.getPassword());
                password.setMaxAge(7 * 24 * 60 * 60);
                password.setHttpOnly(true);
                res.addCookie(phone);
                res.addCookie(password);
                return AjaxResult.success();
            } else {
                return AjaxResult.error(400,"当前账号已冻结");
            }
        } else {
            return AjaxResult.error(400,"用户名或密码不正确");
        }
    }
}
